clear
set more off

use $tempdir/panel_trends_30yr_income.dta, clear

foreach cohort in 30{

*Get data, limit down to sample and relevant variables
*Already limited to individuals in the age range

use $tempdir/panel_trends_`cohort'yr_income.dta, clear
keep if race==1
keep serial pernum statefip datanum region_birth_coarse spouseregion_birth_coarse first year1 year2 statefip spousefirst bpl spousebpl urban age spouseage agegap sex race spouserace /*
*/ married ever_married logocc* qtl* husboccscore byname_* mbpl fbpl occscore

*Generate the absolute difference in spousal income and an individual-level measure of assortativeness
*If statements to make sure we only generate this when both spouses are white and in the age range
*Add different measures of the distance here

foreach var in $incvars{

replace logocc`var'_father_spouse = . if spouseage < `cohort' & spouseage > (`cohort' + 15)
replace logocc`var'_father_reg_spouse = . if spouseage < `cohort' & spouseage > (`cohort' + 15)

gen absdiff_`var' = abs(logocc`var'_father - logocc`var'_father_spouse) if spouserace == 1 & spouseage >= `cohort' & spouseage <= (`cohort' + 15)
gen absdiff_`var'_reg = abs(logocc`var'_father_reg - logocc`var'_father_reg_spouse) if spouserace == 1 & spouseage >= `cohort' & spouseage <= (`cohort' + 15)

}

sort year2 statefip datanum serial pernum

*Save dataset
save ${tempdir}/panelreg_inddata_`cohort'yr.dta, replace




*Repeat with the wealth data

use $tempdir/panel_trends_`cohort'yr_wealth.dta, clear
keep if race==1
keep serial pernum statefip datanum year1 year2 race spouserace age spouseage logweal* qtl*

*Generate the absolute difference in spousal income and an individual-level measure of assortativeness
*If statements to make sure we only generate this when both spouses are white and in the age range
*Add different measures of the distance here

foreach var in $wealthvars{


replace logweal_`var'_father_spouse = . if spouseage < `cohort' & spouseage > (`cohort' + 15)
replace logweal_`var'_father_reg_spouse = . if spouseage < `cohort' & spouseage > (`cohort' + 15)

gen absdiff_weal_`var' = abs(logweal_`var'_father - logweal_`var'_father_spouse) if spouserace == 1 & spouseage >= `cohort' & spouseage <= (`cohort' + 15)
gen absdiff_weal_`var'_reg = abs(logweal_`var'_father_reg - logweal_`var'_father_reg_spouse) if spouserace == 1 & spouseage >= `cohort' & spouseage <= (`cohort' + 15)


}

sort year2 statefip datanum serial pernum

*Merge the income variable back on so everything is in one dataset

merge 1:1 year2 statefip datanum serial pernum using ${tempdir}/panelreg_inddata_`cohort'yr.dta, nogen 
save ${tempdir}/panelreg_inddata_`cohort'yr.dta, replace

}








